/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Graph.java
 *
 * Created on 28-Apr-2010, 15:04:37
 */

package phd.changeidentifiers.testing.display.frames;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

/**
 *
 * @author Max
 */
public class GraphFrame extends javax.swing.JFrame {
    private GraphPanel graph;
    private JFileChooser saveFileChooser;
    /** Creates new form Graph */
    public GraphFrame(GraphPanel graph) {
        initComponents();
        this.graph = graph;
        this.graphPanel.add(graph);
        this.saveGraphMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                openSaveDialog();
            }
        });

        saveFileChooser = new JFileChooser();
        saveFileChooser.setMultiSelectionEnabled(false);
//        saveFileChooser.setFileFilter(new ChangeIdentifierSetFileFilter());
        saveFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    }

    public void openSaveDialog(){
        int returnVal = saveFileChooser.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = saveFileChooser.getSelectedFile();
            int decimal = file.getName().lastIndexOf('.');
            String ext;
            if(decimal > -1 && decimal < file.getName().length()){
                ext = file.getName().substring(decimal+1);
            }else{
                file = new File(file.getAbsolutePath()+".png");
                ext = "png";
            }
            if(!ext.toLowerCase().matches("^(png)|(gif)|(jpg)$")){
                int extIndex = file.getName().lastIndexOf("."+ext);
                file = new File(file.getAbsolutePath().substring(0, extIndex)+".png");
                ext = "png";
            }
            try {
                this.graph.writeImage(file, ext);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        graphPanel = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        saveGraphMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout(1, 0));

        graphPanel.setLayout(new java.awt.GridLayout(1, 0));
        getContentPane().add(graphPanel);

        fileMenu.setText("File");

        saveGraphMenuItem.setText("Save Graph");
        fileMenu.add(saveGraphMenuItem);

        menuBar.add(fileMenu);

        setJMenuBar(menuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu fileMenu;
    private javax.swing.JPanel graphPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem saveGraphMenuItem;
    // End of variables declaration//GEN-END:variables

}
